import React from "react";
import { message } from "antd";
import { post } from "@/Utils/http";
import { connect } from "react-redux";
import axios from "axios";
const loginApi = "/app/mockv1/login";

function Login(props) {
  console.log(props);
  const loginBtn = () => {
    post(loginApi, { username: "superAdmin", password: "1Qa2Ws3Ed" }).then(
      (res) => {
        console.log(res);
        // const ress = res.data;
        window.localStorage.setItem("token", res.token);
        window.localStorage.setItem("Menus", JSON.stringify(res.menus));
        let pathname = props.location.search.substr(1);
        // console.log(pathname);
        props.history.push(pathname);
        props.setUser({
          token: res.token,
          userName: res.name,
          menus: res.menus,
        });
        props.setToken({
          token: res.token,
        });
      }
    );
  };

  return (
    <div>
      Login
      <button onClick={loginBtn}>登錄</button>
    </div>
  );
}

export default connect(
  (store) => store,
  (dispatch) => {
    return {
      setUser(payload) {
        dispatch({ type: "set_user", payload });
      },
      setToken(payload) {
        dispatch({ type: "set_token", payload });
      },
    };
  }
)(Login);
